;
(function (call) {
    var cma3kmlag = {
        mapId: "tjMap",
        selectedPro: [],
        playList: null,
        latestTime: null,
        isFirstGetTime: true,
        router: {
            cfg: "/business/szyb/json/cma3kmlag.json",
        },
        init: function () {
            var that = this;
            $.getJSON(that.router.cfg, function (cfg) {
                if (cfg) {
                    that.buildContainer(cfg);
                    that.bindEvt();
                }
            })
        },
        // 获取最新起报时间
        getLatestTime: function (pro) {
            var that = this;
            that.isFirstGetTime = false;
            that.latestTime = "2023-09-27 20:00:00";
			$(".product-cma3kmlag-datepicker #cma3kDate").IDatePicker().setVal(that.latestTime);
			that.buildLastTime();
        },
		 // 初始化最近起报时间
        buildLastTime: function (call) {
            var baseDate = $("#cma3kDate").IDatePicker().getVal() + ":00";
            var newDate = new Date(baseDate);
            var liArr = [];
            for (var i = 0; i < 4; i++) {
                liArr.push(`<li class="${i == 0 ? 'active' : ''}" data-time="${newDate}">${newDate.format("ddhh")}</li>`);
                newDate.setHours(newDate.getHours() - 20);
            }
            $(".product-cma3kmlag-datechange ul").empty().append(liArr.reverse().join(""));
			if(call){
				call();
			}
        },
        /**
         * 构建弹出框内容
         * 构建弹出框内容
         */
        buildContainer: function (data) {
            var that = this;
            if (!data) {
                return;
            }
			console.log("buildContainer");
			debugger
            data.pro_type.forEach(function (item) {
                var $li = $(`<li>${item.name}</li>`);
                $li.data('model', item);
                $li.off().click(function () {
                    if ($(this).hasClass("active")) {
                        return;
                    }
                    that.hideImgContain(); //切换模式后, 清空之前模式数据
                    $(this).addClass("active").siblings().removeClass("active");

                    $('.product-cma3kmlag-datepicker').empty()
                        .append(`
						<div class="date-panel">
							<input class="DateAgent" id="cma3kDate" data-options="${item.dateOptions}" value="">
							<i class="btn-forward"></i>
							<i class="btn-backward"></i>
							<i class="btn-newest"></i>
						</div>`);
                    $.IDate(".product-cma3kmlag-datepicker .date-panel", {
                        query: function query() {
                            cma3kmlag.buildLastTime(function () {
                                cma3kmlag.showImgContain();
                            });
                        }
                    });
					that.getLatestTime();
					that.buildLastTime();
                    that.buildProContent(data.basic_pro[item.id] || []);
					that.showImgContain();
					//$(".product-cma3kmlag-list ul li:first").click();
                })
                $(".product-cma3kmlag-tab ul").append($li);
            })
            $(".product-cma3kmlag-tab").hide();
			$(".product-cma3kmlag-tab ul li:first").click();
        },
        /**
         * 构建要素展示
         * data 要素列表
         */
        buildProContent: function (data) {
            var that = this;
            $(".product-cma3kmlag-list").empty();
            data.forEach(function (item) {
                var $item = $('<div>').addClass('product-item');
                var $title = $('<div>').addClass('product-title');
                $title.data('type', item.group_id);
                $title.html(item.group_name);
                $item.append($title);
                if (item.content) {
                    var $ul = $('<ul>');
					item.content.forEach(function(pro) {
						var $li = $(`<li><span>${pro.ele_name}</span></li>`);
						if (pro.selected)
							$li.addClass("default");
						$li.data('pro', pro);
						if (pro.title)
							$li.attr('title', pro.title);
						if (pro.width)
							$li.css("width", pro.width);
						$ul.append($li);
					})
					$item.append($ul);
                }
                $(".product-cma3kmlag-list").append($item);
            })
			$(".product-cma3kmlag-list ul li:first").addClass("active");
        },
        showImgContain: function () {
			var that = this;
			that.hideImgContain();
			//http://10.226.120.112:9995/202309/laf-2023092720.006.png
			let time = new Date($(".product-cma3kmlag-datepicker #cma3kDate").IDatePicker().getVal() + ":00");
			let interval = $(".product-cma3kmlag-interval li.active").attr("interval");
			let imgSrc = "http://10.226.120.112:9995/"+time.format('yyyyMM')+"/laf-"+time.format('yyyyMMddhh')+"."+interval+".png";
			
		    CafeJS.ModalBox({
                id: 'cma3kmlag_dialog',
                container: 'cafe-busis-container',
                title: "CMA3km-LAG",
                width: 830,
                height: 630,
                m_top: 55,
                m_left: 600,
                hide: true,
                //url: that.router.initProductPicPop,
				html:'<div><img src="'+imgSrc+'"></img></div>',
                closed: function () {
                    $("#modal_product_pic").remove();
                    $(".product-cma3kmlag-list li").removeClass('active');
                }
            });
        },
        hideImgContain: function () {
          $("#modal_cma3kmlag_dialog").remove();
        },
        /**
         * 绑定事件操作
         */
        bindEvt: function () {
            var that = this;
            // 要素点击事件
            $(".product-cma3kmlag-list").off('click').on('click', 'li', function () {
                var pro = $(this).data('pro');
                if ($(this).hasClass("active")) {
                    $(this).removeClass("active");
					that.hideImgContain();
                } else {
                    $(this).addClass("active");
                    that.showImgContain();
                }
            })
            // 预报时间间隔
            $(".product-cma3kmlag-interval").delegate('li', 'click', function() {
                $(this).addClass("active").siblings().removeClass("active");
                var interval = Number($(this).attr('interval')) * 60;
                that.showImgContain();
            })
            // 最近起报时间
            $(".product-cma3kmlag-datechange").delegate('li', 'click', function () {
                $(this).addClass("active").siblings().removeClass("active");
                var time = $(this).data('time');

                $("#cma3kDate").IDatePicker().setVal(time);

                that.showImgContain();
            })
            $('.product-cma3kmlag-datepicker').off('click').on('click', '.btn-forward', function (e) { // 日期向前
                e.stopPropagation();
                $("#cma3kDate").IDatePicker().toForward();
                that.buildLastTime(function () {
                    that.showImgContain();
                });
            }).on('click', '.btn-backward', function (e) { // 日期向后
                e.stopPropagation();
                $("#cma3kDate").IDatePicker().toBackward();
                that.buildLastTime(function () {
                    that.showImgContain();
                });
            }).on('click', ".btn-newest", function (e) { // 最新
                e.stopPropagation();
                $("#cma3kDate").IDatePicker().toNewest();
                that.buildLastTime(function () {
                    that.showImgContain();
                });
            });
            //$(".product-cma3kmlag-tab li").first().click();
        }
    }
    if (typeof call === 'function') {
        CafeJS.cma3kmlag = cma3kmlag;
        call(cma3kmlag);
    }
    window.cma3kmlag = cma3kmlag;
})(function (cma3kmlag) {
    CafeJS.models(['ajax', 'legentconfig', 'legentpicker', 'timeLine2'], function () {
        cma3kmlag.init();
    });
});
